1. 题目描述(中等难度)

[success] [剑指 Offer 09. 用两个栈实现队列

2. 解法一: 两个栈实现

class CQueue {
    Deque<Integer> A;
    Deque<Integer> B;
    public CQueue() {
      A= new LinkedList<>();
      B= new LinkedList<>();
    }

    public void appendTail(int value) {
      A.offerFirst(value);
    }

    public int deleteHead() {  
       if(B.isEmpty()){
          while(!A.isEmpty()){
            B.offerFirst(A.pollFirst());
          }
       }
       if(B.isEmpty()){
           return -1;
       }
       return B.pollFirst();
    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""